package com.ruoyi.project.credentials.service.impl;

import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.security.LoginUser;
import com.ruoyi.project.system.domain.SysCredentialsType;
import com.ruoyi.project.system.domain.SysUser;
import com.ruoyi.project.system.service.ISysCredentialsTypeService;
import com.ruoyi.project.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.credentials.mapper.CredentialsMapper;
import com.ruoyi.project.credentials.domain.Credentials;
import com.ruoyi.project.credentials.service.ICredentialsService;

/**
 * 用户证书Service业务层处理
 *
 * @author chenweiguo
 * @date 2020-05-05
 */
@Service
public class CredentialsServiceImpl implements ICredentialsService {
    @Autowired
    private CredentialsMapper credentialsMapper;

    @Autowired
    private ISysCredentialsTypeService sysCredentialsTypeService;


    @Autowired
    private ISysUserService userService;


    /**
     * 查询用户证书
     *
     * @param credentialsId 用户证书ID
     * @return 用户证书
     */
    @Override
    public Credentials selectCredentialsById(Long credentialsId) {
        return credentialsMapper.selectCredentialsById(credentialsId);
    }

    /**
     * 查询用户证书列表
     *
     * @param credentials 用户证书
     * @return 用户证书
     */
    @Override
    public List<Credentials> selectCredentialsList(Credentials credentials) {
        // 判断用户是不是管理员
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser user = loginUser.getUser();
        Long userId = user.getUserId();
        Long credentialsTypeId = credentials.getCredentialsTypeId();
        if (credentialsTypeId == null) {
            credentialsTypeId = 100L;
        }
        SysCredentialsType sysCredentialsType = sysCredentialsTypeService.selectSysCredentialsTypeById(credentialsTypeId);
        credentials.setCredentialsTypeLeave(sysCredentialsType.getAncestors() + "," + sysCredentialsType.getCredentialsTypeId());

        if (SysUser.isAdmin(userId)) {
            // 如果是管理员则显示所有证书
            credentials.setUserId(null);
            return credentialsMapper.selectCredentialsList(credentials);
        } else {
            // 如果是普通用户，则只能看自己的证书
            credentials.setUserId(userId);
            return credentialsMapper.selectCredentialsListByOrdinary(credentials);
        }


//        return credentialsMapper.selectCredentialsList(credentials);
    }

    /**
     * 新增用户证书
     *
     * @param credentials 用户证书
     * @return 结果
     */
    @Override
    public int insertCredentials(Credentials credentials) {
        Long userId = credentials.getUserId();
        SysUser sysUser = userService.selectUserById(userId);
        credentials.setUserId(userId);
        credentials.setUserName(sysUser.getUserName());
        // 设置证书类型信息
        Long credentialsId = credentials.getCredentialsTypeId();
        SysCredentialsType sysCredentialsType = sysCredentialsTypeService.selectSysCredentialsTypeById(credentialsId);
        credentials.setCredentialsType(sysCredentialsType.getCredentialsType());
        credentials.setCredentialsTypeLeave(sysCredentialsType.getAncestors());
        credentials.setCreateTime(DateUtils.getNowDate());
        return credentialsMapper.insertCredentials(credentials);
    }

    /**
     * 修改用户证书
     *
     * @param credentials 用户证书
     * @return 结果
     */
    @Override
    public int updateCredentials(Credentials credentials) {
        credentials.setUpdateTime(DateUtils.getNowDate());
        return credentialsMapper.updateCredentials(credentials);
    }

    /**
     * 批量删除用户证书
     *
     * @param credentialsIds 需要删除的用户证书ID
     * @return 结果
     */
    @Override
    public int deleteCredentialsByIds(Long[] credentialsIds) {
        return credentialsMapper.deleteCredentialsByIds(credentialsIds);
    }

    /**
     * 删除用户证书信息
     *
     * @param credentialsId 用户证书ID
     * @return 结果
     */
    @Override
    public int deleteCredentialsById(Long credentialsId) {
        return credentialsMapper.deleteCredentialsById(credentialsId);
    }
}
